package 链表与双指针;

public class LinkedListGenerator {

    // 生成链表的函数
    public static ListNode generateLinkedList(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }
        
        // 创建头节点
        ListNode head = new ListNode(arr[0]);
        ListNode current = head;
        
        // 遍历数组，逐个创建节点并连接起来
        for (int i = 1; i < arr.length; i++) {
            current.next = new ListNode(arr[i]);
            current = current.next;
        }
        
        return head;
    }

    // 打印链表的函数（用于测试）
    public static void printLinkedList(ListNode head) {
        ListNode current = head;
        while (current != null) {
            System.out.print(current.val + " -> ");
            current = current.next;
        }
        System.out.println("null");
    }

    // 测试代码
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        ListNode head = generateLinkedList(arr);
        printLinkedList(head);
    }
}